package com.walkline.screen;

import localization.RedFlowerResource;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.SystemListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.walkline.app.RedFlowerAppConfig;
import com.walkline.util.Functions;

public class BatteryInfoScreen extends PopupScreen implements SystemListener, RedFlowerResource
{
	private static ResourceBundle _resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
	
	public BatteryInfoScreen()
	{
		super(new VerticalFieldManager(),Field.FOCUSABLE);
	
		setBorder(RedFlowerAppConfig.border_Transparent);
		setBackground(RedFlowerAppConfig.bg_Transparent);

		initUI();
    	Application.getApplication().addSystemListener(this);
	}
	
	private void initUI()
	{
		LabelField labelTitle = new LabelField(getResString(BATTERY_INFO_TITLE), USE_ALL_WIDTH | LabelField.HCENTER);
		labelTitle.setPadding(0, 0, 3, 0);
		LabelField labelLevel=new LabelField(getResString(BATTERY_INFO_LEVEL) + DeviceInfo.getBatteryLevel() + "%");
		labelLevel.setPadding(5, 0,0,0);

		add(labelTitle);
		add(new SeparatorField());
		add(labelLevel);
		add(new LabelField(getResString(BATTERY_INFO_VOLTAGE) + Functions.getVolt() + "V"));
		add(new LabelField(getResString(BATTERY_INFO_TEMPERATURE) + DeviceInfo.getBatteryTemperature() + "°C"));

		String batteryStats = new String();
		int _batteryStatus = DeviceInfo.getBatteryStatus();
		
		if (_batteryStatus == DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) {
			batteryStats = getResString(BATTERY_STATUS_CHARGE_COMPLETED);
		} else if ((_batteryStatus & DeviceInfo.BSTAT_CHARGING) == DeviceInfo.BSTAT_CHARGING | 
			(_batteryStatus & DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) == DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER |
			(_batteryStatus & DeviceInfo.BSTAT_AC_CONTACTS) == DeviceInfo.BSTAT_AC_CONTACTS)
		{
			if ((_batteryStatus & DeviceInfo.BSTAT_AC_CONTACTS) == DeviceInfo.BSTAT_AC_CONTACTS) {
				batteryStats = getResString(BATTERY_STATUS_CHARGING_VIA_AC);}
			else if ((_batteryStatus & DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) == DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) {
				batteryStats = getResString(BATTERY_STATUS_CHARGING_VIA_USB);}

			if ((_batteryStatus & DeviceInfo.BSTAT_LOW_RATE_CHARGING) == DeviceInfo.BSTAT_LOW_RATE_CHARGING) {
				batteryStats += getResString(BATTERY_STATUS_LOW_RATE);}
		}
		else if ((_batteryStatus & DeviceInfo.BSTAT_LOW) == DeviceInfo.BSTAT_LOW) {
			batteryStats = getResString(BATTERY_STATUS_LOW);}
		else if ((_batteryStatus & DeviceInfo.BSTAT_NO_RADIO) == DeviceInfo.BSTAT_NO_RADIO) {
			batteryStats = getResString(BATTERY_STATUS_NO_RADIO);}
		else {
			switch (_batteryStatus)
			{
				case 0:
					batteryStats = getResString(BATTERY_STATUS_NORMAL);
					break;
				case DeviceInfo.BSTAT_DEAD:
					batteryStats = getResString(BATTERY_STATUS_DEAD);
					break;
				case DeviceInfo.BSTAT_NONE:
					batteryStats = getResString(BATTERY_STATUS_NO_BATTERY);
					break;
				case DeviceInfo.BSTAT_TOO_COLD:
					batteryStats = getResString(BATTERY_STATUS_TOO_COLD);
					break;
				case DeviceInfo.BSTAT_TOO_HOT:
					batteryStats = getResString(BATTERY_STATUS_TOO_HOT);
					break;
				case DeviceInfo.BSTAT_UNKNOWN_BATTERY:
					batteryStats = getResString(BATTERY_STATUS_UNKNOWN_BATTERY);
					break;
				case DeviceInfo.BSTAT_LOW_RATE_CHARGING:
					batteryStats = getResString(BATTERY_STATUS_LOW_RATE);
					break;
				default:
					batteryStats = String.valueOf(_batteryStatus);
					break;
			}
		}

		LabelField statusLabel = new LabelField(getResString(BATTERY_INFO_STATUS) + batteryStats) {
			protected void paint(Graphics g)
			{
				if((DeviceInfo.getBatteryStatus() & DeviceInfo.BSTAT_LOW_RATE_CHARGING) == DeviceInfo.BSTAT_LOW_RATE_CHARGING)
				{
					g.setColor(Color.RED);
					super.paint(g);
				} else {
					super.paint(g);
				}
			}
		};
		add(statusLabel);
	}
	
	private String getResString(int key) {return _resources.getString(key);}
	
	public boolean onClose()
	{
    	Application.getApplication().removeSystemListener(this);
		UiApplication.getUiApplication().popScreen(this);
		
    	return true;
	}

	protected void paintBackground(Graphics g) {}
	
	protected boolean keyChar(char key, int status, int time)
	{
		onClose();
		return true;
	}

	protected boolean navigationClick( int status, int time ) 
    {
		onClose();
        return true;    
    }

	protected boolean trackwheelClick( int status, int time )
    {        
		onClose();    
        return true;
    }

	protected boolean touchEvent(TouchEvent message)
	{
		if(message.getEvent()==TouchEvent.UNCLICK)
		{
			onClose();
			return true;	
		}

		return super.touchEvent(message);
	}
	
	public void batteryGood() {}
	public void batteryLow() {}
	public void powerOff() {}
	public void powerUp() {}

	public void batteryStatusChange(int status)
	{
		deleteAll();
		initUI();
	}
}